#include "main.h"
#include "ctrl.h"

//初始化时间片，质数是为了尽量在同一时刻下减少时间片重合
static tick_t tick_5ms   = { 0, 5};
static tick_t tick_11ms  = { 0, 11};
static tick_t tick_19ms  = { 0, 19};
static tick_t tick_47ms  = { 0, 47};
static tick_t tick_101ms = { 0, 101};
static tick_t tick_499ms = { 0, 499};
static tick_t tick_997ms = { 0, 997};

static int fsp_num = 0;
static uint32_t time_num = 0;
static char show_str[20] = {0};

void bsp_init(void)
{
	SYSTICK_Init();//1ms的sys定时器
	// LED_init();//初始化GPIO
    uart_init();//UART0驱动初始化
    pwm_init();//pwm初始化
    bftm_init();//bftm初始化
    iic_init();//iic初始化
    OLED_Init();//oled初始化
    
    //更新时间片时间
    tick_refresh(&tick_5ms);
    tick_refresh(&tick_11ms);
    tick_refresh(&tick_19ms);
    tick_refresh(&tick_47ms);
    tick_refresh(&tick_499ms);
    tick_refresh(&tick_997ms);

    uart_send(UART_COM,"ht32f52352 project!\n",20);
    OLED_ShowString(0,0*8,"test",8);
    OLED_ShowString(0,6*8,"bftm:",8);
    OLED_ShowString(0,7*8,"fsp:",8);
    OLED_DrawCircle(64,32,10);
}

//系统时间片轮询
void proc(void)
{
    if(tick_istimeout(&tick_5ms))
    {
        pwm_proc();
        uart_proc();
    }
    if(tick_istimeout(&tick_11ms))
    {
    }
    if(tick_istimeout(&tick_19ms))
    {
    }
    if(tick_istimeout(&tick_47ms))
    {
        MeasureStart();
        OLED_Refresh();
        MeasureGet(time_num);
        fsp_num++;
    }
    if(tick_istimeout(&tick_101ms))
    {
    }
    if(tick_istimeout(&tick_499ms))
    {
    }
    if(tick_istimeout(&tick_997ms))
    {
        // LED_TOGGLE();
        // uart_send(UART_COM,"uart_test!\n",11);
        // OLED_ShowNum(5*6,6*8,time_num,5,8);
        sprintf(show_str,"%3.2f ms",((float)time_num)/((float)SystemCoreClock/1000));
        OLED_ShowString(5*6,6*8,show_str,8);
        OLED_ShowNum(4*6,7*8,fsp_num,3,8);
        fsp_num = 0;
        WDT_Restart(); 
    }
}
